<?
/**
 * FullSearchContent for Core
 * Version 2.0
 */

class FullSearchContent 
{
	public $words = array();
	
	private $words_idx = array();

	private $bold_idx = array();
	
	public $words_per_line = 20;

	public $lines = 2;


	function __construct($content='') {
		$this->words = preg_split('/\s+/',$content);
		for ($i=0;$i<count($this->words);$i++) {
			$this->words_idx[$i] = array();
			$this->words_idx[$i][bold] = preg_match('/^<b>/',$this->words[$i]);
			if ($this->words_idx[$i][bold]) {
				array_push($this->bold_idx,$i);
			}
			$this->words_idx[$i][length] = strlen($this->words[$i]);
			$this->words_idx[$i][end] = preg_match('/\.$/',$this->words[$i]);
		}
	}
	
	function make_content () {
		$last = 0;
		$arr = array();
		$result = array();
		$words_per_line = $this->words_per_line;
		#print_r($this->bold_idx);
		
		foreach ($this->bold_idx as $i) {
			#print "last = $last; i = $i\n";
			if (($i - $last) > $words_per_line) {
				#print "Ok1\n";
				for($j=$last;$j<=$i;$j++) {
					if ($this->words_idx[$j][end]) {
						$last=$j;
					}
				}
				
				if (($i - $last) > $words_per_line) {
						array_push($arr,-1);
						$last = $i - round($words_per_line/2);
				}
				
			} 
			
			for($j=$last;$j<=$i;$j++) {
				array_push($arr,$j);
			}
			#print_r($arr);
			
			$last = $i+1;
			if (count($arr) >= $this->words_per_line) {
				$words_per_line = $this->words_per_line;
				array_push($result,$arr);
				$arr = array();
			} else {
				$words_per_line = $this->words_per_line - count($arr);
			}
			
		}
		if (count($this->bold_idx) == 0) {
			$last--;
		}
		#print ($last+1).' - '.($last+1+($this->words_per_line-count($arr)))."\n";
		if (count($arr) < $this->words_per_line) {
			for($j=$last+1;$j<=$last+1+($this->words_per_line-count($arr));$j++) {
				array_push($arr,$j);
			}
			array_push($result,$arr);
		}
		
		$lines_result = array();
		foreach ($result as &$line_arr) {
			$line = '';
			foreach ($line_arr as $idx) {
				if ($idx < 0) {
					$line .= '... ';
				} else {
					$line .= $this->words[$idx].' ';
				}
			}
			array_push($lines_result,$line);
		}
		
		#print_r($lines_result);
		
		return $lines_result;
		#print_r($this->words);
		#print_r($this->words_idx);
	}
	
}